package com.gh.ggkt.activity.service.impl;

import com.atguigu.ggkt.model.order.OrderDetail;
import com.atguigu.ggkt.model.order.OrderInfo;
import com.atguigu.ggkt.vo.order.OrderInfoQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.ggkt.activity.mapper.OrderInfoMapper;
import com.gh.ggkt.activity.service.OrderDetailService;
import com.gh.ggkt.activity.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 订单表 服务实现类
 * </p>
 *
 * @author Eric Fang
 * @since 2022-09-18
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 订单列表
     *
     * @param pageParam
     * @param orderInfoQueryVo
     * @return
     */
    @Override
    public Map<String, Object> pageList(Page<OrderInfo> pageParam, OrderInfoQueryVo orderInfoQueryVo) {
        //获取查询条件
        Long userId = orderInfoQueryVo.getUserId();//用户id
        String outTradeNo = orderInfoQueryVo.getOutTradeNo();//交易号
        String phone = orderInfoQueryVo.getPhone();//手机号
        Integer orderStatus = orderInfoQueryVo.getOrderStatus();
        String createTimeBegin = orderInfoQueryVo.getCreateTimeBegin();
        String createTimeEnd = orderInfoQueryVo.getCreateTimeEnd();
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(OrderInfo::getUserId, userId);
        }
        if (StringUtils.isNotEmpty(outTradeNo)) {
            queryWrapper.eq(OrderInfo::getOutTradeNo, outTradeNo);
        }
        if (StringUtils.isNotEmpty(phone)) {
            queryWrapper.eq(OrderInfo::getPhone, phone);
        }
        if (StringUtils.isNotEmpty(createTimeBegin)) {
            queryWrapper.ge(OrderInfo::getCreateTime, createTimeBegin);
        }
        if (StringUtils.isNotEmpty(createTimeEnd)) {
            queryWrapper.le(OrderInfo::getCreateTime, createTimeEnd);
        }
        if (orderStatus != null) {
            queryWrapper.eq(OrderInfo::getOrderStatus, orderStatus);
        }
        Page<OrderInfo> orderInfoPage = baseMapper.selectPage(pageParam, queryWrapper);
        long total = orderInfoPage.getTotal();
        long pages = orderInfoPage.getPages();
        List<OrderInfo> orderInfoList = orderInfoPage.getRecords();
        orderInfoList.forEach(item -> {
            this.getOrderDetail(item);
        });
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("pageCount", pages);
        map.put("orderInfoList", orderInfoList);
        return map;
    }

    /**
     * 查询订单详情数据
     */
    private OrderInfo getOrderDetail(OrderInfo orderInfo) {
        Long id = orderInfo.getId();
        OrderDetail orderDetail = orderDetailService.getById(id);
        if (orderDetail != null) {
            String courseName = orderDetail.getCourseName();
            orderInfo.getParam().put("courseName", courseName);
        }
        return orderInfo;
    }
}
